package com.dao.impl;

import com.bean.User;
import com.dao.UserDao;

import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public User existsUserid(String userid) {
        String sql="select `userid`,`pet_name`,`head_sculpture`,`relname`,`postal_code`,`telephone`,`password`,`address`,`sex` from user where userid=?";
        return queryForOne(User.class,sql,userid);
    }

    @Override
    public User login(String userid, String password) {
        String sql="select `userid`,`pet_name`,`head_sculpture`,`relname`,`postal_code`,`telephone`,`password`,`address`,`sex` from user where userid=? and password=?";
        return queryForOne(User.class,sql,userid,password);
    }

    @Override
    public int saveUser(User user) {
        String sql="insert into user(`userid`,`pet_name`,`head_sculpture`,`relname`,`postal_code`,`telephone`,`password`,`address`,`sex`)values(?,?,?,?,?,?,?,?,?)";
        return update(sql,user.getUserid(),user.getPet_name(),user.getHead_sculpture(),user.getRelname(),user.getPostal_code(),user.getTelephone(),user.getPassword(),user.getAddress(),user.getSex());
    }

    @Override
    public int deleteByUserid(String userid) {
        String sql="delete from user where userid=?";
        return update(sql,userid);
    }

    @Override
    public int updatePass(String userid,String password) {
        String sql="update user set `password`=?where userid=?";
        return update(sql,password,userid);
    }

    @Override
    public int updateMess(String userid,String pet_name,String relname,String postal_code,String telephone,String address,int sex) {
        String sql="update user set `pet_name`=?,`relname`=?,`sex`=?,`postal_code`=?,`telephone`=?,`address`=? where userid=?";
        return update(sql,pet_name,relname,sex,postal_code,telephone,address,userid);
    }

    @Override
    public int updatePhoto(String userid, String head_sculpture) {
        String sql="update user set `head_sculpture`=? where userid=?";
        return update(sql,head_sculpture,userid);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from user";
        Number count= (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<User> queryForPageItem(int begin, int pageSize) {
        String sql="select `userid`,`pet_name`,`head_sculpture`,`relname`,`postal_code`,`telephone`,`password`,`address` from user limit ?,?";
        return queryForList(User.class,sql,begin,pageSize);
    }

    @Override
    public User showMess(String enterpriseid) {
        String sql="select `userid`,`pet_name`,`head_sculpture`,`relname`,`postal_code`,`telephone`,`password`,`address`,`sex` from user where userid=?";

        return queryForOne(User.class,sql,enterpriseid);
    }
}
